
/* 
 * File:   Packer.cpp
 * Author: saulio
 * Created on April 18, 2012, 3:12 PM
 */

#include "Frame.h"


int main() {
    
    key_t key;
    int packer_queue_id;
    message_queue outbound_mail;
    
    key = ftok ("/bin/ls", S_PACKER_QUEUE_KEY);
	if (key == (key_t)-1)
	{
		std::cout << "Error al obtener clave para cola mensajes" << std::endl;
		return(EXIT_FAILURE);
	} 
    
        packer_queue_id = msgget (key, 0600 | IPC_CREAT);
	if (packer_queue_id == -1)
	{
		std::cout << "Error al obtener identificador para cola mensajes" << std::endl;
		return(EXIT_FAILURE);
	}
   
     struct msghdr mh;
     struct iovec v[1];
     struct sockaddr_in cli_addr;
     int sockfd, n_bytes_w;
     package message;
    
     bzero((char *) &cli_addr, sizeof(cli_addr));
     bzero((char *) &message, sizeof(message));
     bzero((char *) &mh, sizeof(mh));
     
     sockfd = socket(AF_INET, SOCK_DGRAM, 0);
     v[0].iov_base = &message;
     v[0].iov_len = sizeof(message);
   
     mh.msg_name = &cli_addr;
     mh.msg_namelen = sizeof(cli_addr);
     mh.msg_iov = v;
     mh.msg_iovlen = 1;
     
     std::cout <<"Inicializando el SERVIDOR DE EMPAQUETADO" << std::endl
               <<"El id del packer queue es: "<< packer_queue_id << std::endl;
     
     while(true){
       
         sleep(1);
         if(msgrcv(packer_queue_id, (struct msgbuf *)&outbound_mail,
         sizeof(outbound_mail.package)+sizeof(outbound_mail.cli_addr),HIGHT_PRIORITY,IPC_NOWAIT)>0)
         {
             std::cout<<"Mensaje de alta prioridad"<<std::endl;
             message=outbound_mail.package;
             cli_addr.sin_addr.s_addr = outbound_mail.cli_addr.sin_addr.s_addr;
             cli_addr.sin_port = htons(QUIMERA_PORT);
             if ((n_bytes_w = sendmsg(sockfd, &mh, 0))< 0)
                 std::cout<<"Error al escribir el Socket"<<std::endl;
                 
             print_output(outbound_mail.package);
         }
         
         else if(msgrcv(packer_queue_id, (struct msgbuf *)&outbound_mail,
         sizeof(outbound_mail.package)+sizeof(outbound_mail.cli_addr),MEDIUM_PRIORITY,IPC_NOWAIT)>0)
         {
             std::cout<<"Mensaje de media prioridad"<<std::endl;
             message=outbound_mail.package;
             cli_addr.sin_addr.s_addr = outbound_mail.cli_addr.sin_addr.s_addr;
             cli_addr.sin_port = htons(QUIMERA_PORT);
             if ((n_bytes_w = sendmsg(sockfd, &mh, 0))<0)
                 std::cout<<"Error al escribir el Socket"<<std::endl;
                 
             print_output(outbound_mail.package);
         }
         
         else if(msgrcv(packer_queue_id, (struct msgbuf *)&outbound_mail,
         sizeof(outbound_mail.package)+sizeof(outbound_mail.cli_addr),LOW_PRIORITY,IPC_NOWAIT)>0)
         {
             std::cout<<"Mensaje de baja prioridad"<< std::endl;
             message=outbound_mail.package;
             cli_addr.sin_addr.s_addr = outbound_mail.cli_addr.sin_addr.s_addr;
             cli_addr.sin_port = htons(QUIMERA_PORT);
             if ((n_bytes_w = sendmsg(sockfd, &mh, 0))<0)
                 std::cout<<"Error al escribir el Socket"<<std::endl;
                 
             print_output(outbound_mail.package);
         }  
        }
     close(sockfd);
     return (EXIT_FAILURE);  
}
    

